app.controller('RoleAuthorityCtrl', function ($scope, $modalInstance, $rootScope, toaster, param, ajax) {

	var inserter = new Req.POST({ resource: $scope.resource });

	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}


	$scope.filterTitle = function (title) {
		console.log(title.moduleId);
		if (title.moduleId != 0) {
			return true;
		} else {
			return false;
		}
	}

	$scope.rolelist = $scope.param.auth_list;

	$scope.stafflist = [];

	$scope.authlist = [];

	(function () {
		ajax.call(new Req.GET({
			resource: 'role/auth/' + param.data.id + '/list'
		})).then(function (result) {
			$scope.stafflist = result.result;
			$scope.authlist = [];
			var maxlength = $scope.stafflist.length;
			var count = 0;
			var code1;
			for (var i = 0; i < $scope.rolelist.length; i++) {
				if (count == maxlength) {
					code1 = "";
				} else {
					code1 = $scope.stafflist[count].authority.code;
				}
				var code2 = $scope.rolelist[i].code;
				if (code1 != code2) {
					$scope.authlist.push({
						id: $scope.rolelist[i].id,
						code: $scope.rolelist[i].code,
						moduleId: $scope.rolelist[i].moduleId,
						name: $scope.rolelist[i].name,
						description: $scope.rolelist[i].description,
						menu: $scope.rolelist[i].menu,
						own: false
					});
				} else {
					$scope.authlist.push({
						id: $scope.rolelist[i].id,
						code: $scope.rolelist[i].code,
						moduleId: $scope.rolelist[i].moduleId,
						name: $scope.rolelist[i].name,
						description: $scope.rolelist[i].description,
						menu: $scope.rolelist[i].menu,
						own: true
					});
					count++;
				}
			}
			for (var i = 0; i < $scope.authlist.length; i++) {
				if ($scope.authlist[i].description == "一级菜单") {
					$scope.titleList.push($scope.authlist[i]);
				}
			}
			if ($scope.titleList.length > 0) {
				for (var i = 0; i < $scope.titleList.length; i++) {
					if ($scope.titleList[i].code == "AUTH_BOOKING") {
						$scope.booking = $scope.titleList[i];
					}
					if ($scope.titleList[i].code == "AUTH_AGENCY") {
						$scope.agency = $scope.titleList[i];
					}
					if ($scope.titleList[i].code == "AUTH_RENT") {
						$scope.rent = $scope.titleList[i];
					}
					if ($scope.titleList[i].code == "AUTH_BUS") {
						$scope.bus = $scope.titleList[i];
					}
					if ($scope.titleList[i].code == "AUTH_SYSTEM") {
						$scope.system = $scope.titleList[i];
					}
				}
			}
		}, function () { });
	})();


	$scope.titleList = []; //一级菜单

	$scope.menuList = [];  //二级菜单

	$scope.dataList = [];

	$scope.flag = false;


	$scope.showList = function (auth, obj) {
		$(obj.currentTarget).addClass("bg-info").siblings().removeClass("bg-info");
		$(".second-menu li").removeClass("bg-info");
		$(".third-menu li").removeClass("bg-info");
		$scope.menuList = [];
		$scope.dataList = [];
		for (var i = 0; i < $scope.authlist.length; i++) {
			if ($scope.authlist[i].moduleId == auth.moduleId && $scope.authlist[i].description == "二级菜单" && $scope.authlist[i].code.indexOf(auth.code) >= 0) {
				$scope.menuList.push($scope.authlist[i]);
			}
		}
		$scope.dataList.push(auth);
		$scope.flag = true;
	}


	$scope.showAuth = function (auth, obj) {
		$(obj.currentTarget).addClass("bg-info").siblings().removeClass("bg-info");
		$(".third-menu li").removeClass("bg-info");
		$scope.dataList = [];
		for (var i = 0; i < $scope.authlist.length; i++) {
			if ($scope.authlist[i].menu == auth.menu && $scope.authlist[i].code.indexOf(auth.code) >= 0) {
				$scope.dataList.push($scope.authlist[i]);
			}
		}
	};

	$scope.check = function (auth) {
		if (auth.own) {
			auth.own = false;
		} else {
			auth.own = true;
		}
	};


	$scope.check_all = function () {
		for (var i = 0; i < $scope.dataList.length; i++) {
			$scope.dataList[i].own = true;
		}
	};

	$scope.check_invert = function () {
		for (var i = 0; i < $scope.dataList.length; i++) {
			$scope.dataList[i].own = false;
		}
	};


	$scope.ids = [];
	$scope.ok = function () {
		for (var i = 0; i < $scope.authlist.length; i++) {
			if ($scope.authlist[i].own) {
				$scope.ids.push($scope.authlist[i].id);
			}
		}
		if ($scope.ids != "" && $scope.ids != null && $scope.ids != undefined) {
			inserter.datalist = $scope.ids;
			inserter.resource = 'role/' + param.data.id + '/auth/grant';
			ajax.call(inserter).then(function (result) {
				if (result.code == 200) {
					toaster.pop('success', '成功', '分配权限成功。');
				} else {
					toaster.pop('error', '错误', result.result);
				}
			}, function () {
				toaster.pop('error', '错误', '分配权限失败！');
			});
			$modalInstance.dismiss('cancel');
		} else {
			toaster.pop('warning', '提示', '请选择权限！');
			$scope.submitting = false;
			return;
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});